package cn.hp.beauty.controller;

import cn.hp.beauty.domain.UserInfo;
import cn.hp.beauty.service.UserInfoService;


import cn.hp.beauty.util.Result;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
@Api(tags = "userinfo",value = "/userinfo")
public class UserinfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询所有用户信息
     * @param userInfo
     * @return
     */
    @GetMapping("find")
    @CrossOrigin
    @ApiOperation(value = "查找用户",notes = "查找用户")
    public Result find(UserInfo userInfo){
        //模拟取出所有用户的信息
        List<UserInfo> list=userInfoService.findList(userInfo);
        return Result.success(list);
    }

    /**
     * 新增用户信息
     * @param userInfo
     * @return
     */
    @PostMapping("/insert")
    @CrossOrigin
    @ApiOperation(value = "新增用户",notes = "新增用户")
    public Result insert(@RequestBody UserInfo userInfo){

        System.out.println(userInfo);
        int i = userInfoService.insertUserinfo(userInfo);
        return i > 0 ?Result.success("新增成功"):Result.error("新增失败");
    }

    /**
     * 修改用户信息
     * @param userInfo
     * @return
     */
    @PutMapping("/update")
    @CrossOrigin
    @ApiOperation(value = "修改用户",notes = "修改用户")
    public Result update(@RequestBody UserInfo userInfo){
        int i = userInfoService.updateUserinfo(userInfo);
        return i > 0 ?Result.success("修改成功"):Result.error("修改失败");
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @CrossOrigin
    @ApiOperation(value = "删除用户",notes = "删除用户")
    public Result delete(@PathVariable @ApiParam("被删除用户ID") Integer id){
        int i = userInfoService.deleteById(id);
        return i > 0 ?Result.success("删除成功"):Result.error("删除失败");
    }
}
